#ifndef _GLOBE_H
#define _GLOBE_H



#include "errno.h"
#include "mhdef.h"
#include "dualram.h"
#include <pthread.h>
#include "cmd.h"


#define MH_SYNC()               sync()

#define PI                      3.1415926
#define INCH                    2.54

//运行中修改参数
#define CHANGE_PARAM_CLEAR_FINISHED       1


#define GAUZE_SYS1_BASIC	           81
#define GAUZE_SYS2_BASIC	           84
#define MHFCRS_LOCK_CAM_BYTE           99
#define MHFCRS_LOCK_1CAM2_FLAG         (0xAA)
#define MHFCRS_LOCK_2CAM1_FLAG         (0xBB)

#define MHFCRS_NEW_FLAG_BYTE           98
#define MHFCRS_NEW_FLAG                (0xAA)
#define GAUZE_SYS1_EXTRA               80//5
#define GAUZE_SYS2_EXTRA               83//9
#define GAUZE_SYS3_EXTRA               86
#define GAUZE_SYS4_EXTRA               89
#define ARM2DSP_GAUZE_SYS1_EXTRA       28
#define ARM2DSP_GAUZE_SYS2_EXTRA       29
#define ARM2DSP_GAUZE_SYS1_EXTRA_NEXT  30
#define ARM2DSP_GAUZE_SYS2_EXTRA_NEXT  31
#define ARM2DSP_GAUZE9_STOPL          128
#define ARM2DSP_GAUZE9_STOPH          129


#if(MH_SYS_CAM == 1)
#define SYS_STYE_CAM    1
#else
#define SYS_STYE_CAM    2
#endif


#define IS_NEW_FORMAT(buf)      (((buf[4]) == 0xAA) || ((buf[128 + 98]) == 0xAA))
#define TOGGLE16(datal,datah)   ((datal) | (datah << 8))

#ifdef DUAL_RAM
#define TOUCHSCREEN                  1
//#define TS_PENMOUNT                  1
#define IDS_MH_KEYDEV     "/dev/input/event2"
#else
#ifdef CAN_MSG
#define IDS_MH_KEYDEV     "/dev/input/event1"
#else
#define MOUSE_EVENT                  1
#define IDS_MH_KEYDEV     "/dev/input/event3"
#endif
#endif

#ifdef DUAL_RAM
#define  NEW_KEY_EVENT               1
#endif

#ifdef DUAL_RAM
#define HOT_PLUG_CHECK               1
#endif

extern BYTE speed_array[192];


//使用的段数
struct
{
    int speed_used;   //主速度
    int rab_used;     //主拉布
    int auxrab_used;  //副拉布
    int dumu_used[4]; //度目
    int board_used;   //起底板拉力
    int dmbz_used;    //度目补正
    int sink_used;    //辛克
    int rude_used;      //粗目
} g_sect_used;
//int g_run_state;
#include <sys/time.h>


#define THREAD_WINDOW          1
#define NEED_FLUSH             1
#define NOT_NEED_FLUSH         0
#ifdef THREAD_WINDOW
void window_update_request(int handle, int type);

char g_save_screen_draw;
int g_warn;
int g_focus;
int g_handle_draw;
int g_need_flush;
pthread_mutex_t draw_mutex;
int g_update_handle;
int g_update_last_handle;
#endif

//error num
#define MH_ERROR_NONE           0
#define MH_ERROR_JZ1            1
#define MH_ERROR_JZ2            2
#define MH_ERROR_JZ3            3
#define MH_ERROR_JZ4            4
#define MH_ERROR_JD1            5
#define MH_ERROR_JD2            6
#define MH_ERROR_CAN            7
#define MH_ERROR_QDBGZ          8
#define MH_ERROR_QDBSJ          9
#define MH_ERROR_DUALRAM        10
#define MH_ERROR_GLL            11
#define MH_ERROR_JZJD           12
#define MH_ERROR_WAIT           13
#define MH_ERROR_INVALIDNUM     14
#define MH_ERROR_GLLCLOSED      15
#define MH_ERROR_GLLOPENED      16
#define MH_ERROR_SINK_FRONT     17
#define MH_ERROR_SINK_BACK      18
#define MH_ERROR_YARN           19
#define MH_ERROR_SJDCT          20
#define MH_ERROR_XZQ            21
#define MH_ERROR_DUMUF1         22
#define MH_ERROR_DUMUF2         23
#define MH_ERROR_DUMUB1         24
#define MH_ERROR_DUMUB2         25
#define MH_ERROR_SINKF          26
#define MH_ERROR_SINKB          27
#define MH_ERROR_DUMUF3         28
#define MH_ERROR_DUMUF4         29
#define MH_ERROR_DUMUB3         30
#define MH_ERROR_DUMUB4         31
#define MH_ERROR_EXTPIECE       32
#define MH_ERROR_RESETDONE      33
#define MH_ERROR_NOSINK         34
#define MH_ERROR_CAMF_MOTOR 	35//前三角电机动作错误
#define MH_ERROR_CAMB_MOTOR 	36//后三角电机动作错误
#define MH_ERROR_RAB_SWITCH 	37
#define MH_ERROR_NET_SEND       38      /* 网络发送失败 */
#define MH_ERROR_NET_RECV       39      /* 网络发送失败 */
#define MH_ERROR_CAMF1          40
#define MH_ERROR_CAMB1          41
#define MH_ERROR_CAMF2          42
#define MH_ERROR_CAMB2          43

#define MH_ERROR_RAB_OPEN 	    46//拉布打开失败
#define MH_ERROR_RAB_CLOSE 	    47//拉布合上失败
#define MH_ERROR_YARN_CLIP      48//2个系统的纱嘴冲突
#define MH_ERROR_SINKFR         48
#define MH_ERROR_SINKBR         49
#define MH_ERROR_DUMUF1R        50
#define MH_ERROR_DUMUF2R        51
#define MH_ERROR_DUMUB1R        52
#define MH_ERROR_DUMUB2R        53

#define MH_ERROR_CAMFL          54
#define MH_ERROR_CAMBL          55
#define MH_ERROR_CAMFR          56
#define MH_ERROR_CAMBR          57

#define MH_ERROR_LYCM1          70      /* 左纱嘴电机1 */
#define MH_ERROR_LYCM2          71
#define MH_ERROR_LYCM3          72
#define MH_ERROR_LYCM4          73
#define MH_ERROR_RYCM1          74
#define MH_ERROR_RYCM2          75
#define MH_ERROR_RYCM3          76
#define MH_ERROR_RYCM4          77

#define MH_ERROR_DUMUF5         78
#define MH_ERROR_DUMUF6         79
#define MH_ERROR_DUMUF7         80
#define MH_ERROR_DUMUF8         81
#define MH_ERROR_DUMUB5         82
#define MH_ERROR_DUMUB6         83
#define MH_ERROR_DUMUB7         84
#define MH_ERROR_DUMUB8         85
#define MH_ERROR_RBSINK         86
#define MH_ERROR_RFSINK         87

#define MH_ERROR_FSINK1         88
#define MH_ERROR_BSINK1         89
#define MH_ERROR_FSINK2         90
#define MH_ERROR_BSINK2         91


#define CON_LINE_BYTES             182
#define CNT_LINE_BYTES             128

#define MH_ACT_NONE                0x00
#define MH_TRANSFER                0x01
#define MH_RECEIVE                 0x04
#define MH_TRAN_RECV               0x06
#define MH_KNIT_RECV               0x07
#define MH_KNIT                    0x08
#define MH_TUCK                    0x0A
#define MH_RUDE                    0x0B
#define MH_KNIT_RUDE               0x0C
#define MH_KNIT_TUCK               0x0D
#define MH_KNIT_TRAN               0x10
#define MH_KNIT_JI                 0x19
#define MH_KNIT_TUCK2              24
#define MH_TUCK2                   22
#define MH_PRESS                   20



//error num
#define ERROR_NONE                 0
#if MH_SYS_CAM == 1
#define ERR_SINKB                  1
#define ERR_DUMUB4                 7
#define ERR_DUMUB3                 8
#define ERR_DUMUF4                 9
#define ERR_DUMUF3                 10
#define ERR_SINKF                  2
#define ERR_DUMUB2                 3
#define ERR_DUMUB1                 4
#define ERR_DUMUF2                 5
#define ERR_DUMUF1                 6
#else
#define ERR_SINKB                  1
#define ERR_DUMUB4                 2
#define ERR_DUMUB3                 3
#define ERR_DUMUF4                 4
#define ERR_DUMUF3                 5
#define ERR_SINKF                  6
#define ERR_DUMUB2                 7
#define ERR_DUMUB1                 8
#define ERR_DUMUF2                 9
#define ERR_DUMUF1                 10
#endif

#if 0
#define ERROR_DUMU                 51
#define ERROR_SINK                 52
#define ERROR_YARN                 53
#define ERROR_DRIVE                54
#define ERROR_XZQ                  55
#define ERROR_JIJIA                56
#define ERROR_JITOU                57
#define ERROR_DRIVE_ROCK           58
#define ERROR_DRIVE_JITOU          59
#define ERROR_SHAKE                60
#define ERROR_NEEDLE_SET           61
#define ERROR_SJDCT                62
#define ERROR_STOP                 63
#define ERROR_ROCK                 64
#define ERROR_SHAKE_CANCEL         65
#define ERROR_DUALRAM              99
#define ERROR_CAN                  100
#define ERROR_EXTPIECE             101
#define ERROR_RESETDONE            102
#define ERROR_NOSINK               103
#endif

#define IFLINK		0

#define IDS_MH_TSDEV      "/dev/event1"
#define IDS_MH_MSDEV      "/dev/input/mice"
//#define MH_TSCAL          "/etc/mh_pointercal"
#define MH_TSCAL          "/root/etc/mh_pointercal"
#define MH_ENCODE_FILE    "/etc/dcl"//"/usr/lib/dcl"

#ifdef MH_DEBUG
#define DEBUG_OUT(format,...) printf(format,##__VA_ARGS__)
#else
#define DEBUG_OUT(format,...)
#endif

#define COPYPARAM_DUMU       1
#define COPYPARAM_SUDU       2
#define COPYPARAM_GSP        4

#if MH_SYS_CAM == 1
#define HARDDISK_DIR        "flower/"
#else
#define HARDDISK_DIR        "flower2/"
#endif

//jijia xhfk
#define XHFK_LEFT_SSHA_DSHA(xhfk)    (xhfk[2] & 0x01)
#define XHFK_RIGHT_SSHA_DSHA(xhfk)   (xhfk[2] & 0x02)
#define XHFK_TIANXIAN_DSHA(xhfk)     (xhfk[2] & 0x04)
#define XHFK_SJIE_SLOW(xhfk)         (xhfk[2] & 0x08)
#define XHFK_STOP_POINT(xhfk)        (xhfk[2] & 0x10)
#define XHFK_SLOW_POINT(xhfk)        (xhfk[2] & 0x20)
#define XHFK_QUICK_POINT(xhfk)       (xhfk[2] & 0x40)
#define XHFK_LUOBU(xhfk)             (xhfk[2] & 0x80)

#define XHFK_LEFT_END(xhfk)          (xhfk[3] & 0x01)
#define XHFK_RIGHT_END(xhfk)         (xhfk[3] & 0x02)
#define XHFK_LEFT_DSHA(xhfk)         (xhfk[3] & 0x04)
#define XHFK_RIGHT_DSHA(xhfk)        (xhfk[3] & 0x08)
#define XHFK_FRONT_SAFE(xhfk)        (xhfk[3] & 0x10)
#define XHFK_BACK_SAFE(xhfk)         (xhfk[3] & 0x20)
#define XHFK_ROCK_START(xhfk)        (xhfk[3] & 0x40)
#define XHFK_ROCK_END(xhfk)          (xhfk[3] & 0x80)

#define XHFK_JUANBU(xhfk)            (xhfk[4] & 0x01)
#define XHFK_ZHEN_ZERO(xhfk)         (xhfk[4] & 0x02)
#define XHFK_RAB_JUANBU(xhfk)        (xhfk[4] & 0x04)
#define XHFK_PAUSE(xhfk)             (xhfk[4] & 0x08)
#define XHFK_CHUSQI(xhfk)            (xhfk[4] & 0x10)
#define XHFK_ROCK2_ZERO(xhfk)        (xhfk[4] & 0x40)
#define XHFK_UPS(xhfk)               (xhfk[6] & 0x01)//(xhfk[4] & 0x20)

#define XHFK_LUOBU_RAY(xhfk)         (xhfk[5] & 0x01)
#define XHFK_BIANMA_A(xhfk)          (xhfk[5] & 0x02)
#define XHFK_BIANMA_B(xhfk)          (xhfk[5] & 0x04)
#define XHFK_LEFT_XJIN(xhfk)         (xhfk[5] & 0x08)
#define XHFK_RIGHT_XJIN(xhfk)        (xhfk[5] & 0x10)
#define XHFK_LUOLA_KAIHE(xhfk)       (xhfk[5] & 0x20)
#define XHFK_FRONT_SHOCK(xhfk)       (xhfk[5] & 0x40)
#define XHFK_BACK_SHOCK(xhfk)        (xhfk[5] & 0x80)
//jitou drive
#define XHFK_JITOU_DRIVE(xhfk)       (xhfk[6] & 0x10)
//rock
#define XHFK_ROCK_DRIVE(xhfk)       (xhfk[7] & 0x01)
//ljmd sensor
#define XHFK_MADA_SENSOR(xhfk)       ((xhfk[9] << 8) | xhfk[8])
//jitou xhfk
#if MH_SYS_CAM == 1
#define XHFK_BACK_SINK(xhfk)         (xhfk[10] & 0x01)
#define XHFK_FRONT_SINK(xhfk)        (xhfk[10] & 0x02)
#define XHFK_DUMU_BACK2(xhfk)        (xhfk[10] & 0x04)
#define XHFK_DUMU_BACK1(xhfk)        (xhfk[10] & 0x08)
#define XHFK_DUMU_FRONT2(xhfk)       (xhfk[10] & 0x10)
#define XHFK_DUMU_FRONT1(xhfk)       (xhfk[10] & 0x20)
#else
#define XHFK_BACK_SINK(xhfk)         (xhfk[10] & 0x01)
#define XHFK_DUMU_BACK4(xhfk)        (xhfk[10] & 0x02)
#define XHFK_DUMU_BACK3(xhfk)        (xhfk[10] & 0x04)
#define XHFK_DUMU_FRONT4(xhfk)       (xhfk[10] & 0x08)
#define XHFK_DUMU_FRONT3(xhfk)       (xhfk[10] & 0x10)
#define XHFK_FRONT_SINK(xhfk)        (xhfk[10] & 0x20)
#define XHFK_DUMU_BACK2(xhfk)        (xhfk[10] & 0x40)
#define XHFK_DUMU_BACK1(xhfk)        (xhfk[10] & 0x80)

#define XHFK_DUMU_FRONT2(xhfk)       (xhfk[11] & 0x01)
#define XHFK_DUMU_FRONT1(xhfk)       (xhfk[11] & 0x02)
#endif
#define YARN_FINISH(xhfk)            (xhfk[11] & 0x04)
#define SANJIAO_FINISH(xhfk)         (xhfk[11] & 0x08)
#define XZQ_FINISH(xhfk)             (xhfk[11] & 0x10)
#define XHFK_BACK_PROBE(xhfk)        (xhfk[11] & 0x20)
#define XHFK_FRONT_PROBE(xhfk)       (xhfk[11] & 0x40)
#define XHFK_RIGHT2LEFT(xhfk)        (xhfk[11] & 0x80)

#define XHFK_LEFT2RIGHT(xhfk)        (xhfk[12] & 0x01)
#define XHFK_YARN_24V(xhfk)          (xhfk[12] & 0x02)
#define XHFK_XZQ_24N(xhfk)           (xhfk[12] & 0x04)
#define XHFK_XZQ_24P(xhfk)           (xhfk[12] & 0x08)
#define XHFK_DUMU_24V(xhfk)          (xhfk[12] & 0x10)

#define CAN_ERROR(xhfk)              (xhfk[13] & 0x80)
//qdb
#define XHFK_QDB_HOOK(xhfk)          (xhfk[20] & 0x01)
#define XHFK_QDB_SJ(xhfk)            (xhfk[20] & 0x04)

#define XHFK_GOUZHEN_CLOSE(xhfk)     (xhfk[22] & 0x02)
#define XHFK_GOUZHEN_OPEN(xhfk)      (recv[21] & 0x02)
#define XHFK_CLIP_ONE(xhfk)          (recv[22] & 0x80)
#define XHFK_CLIP_TWO(xhfk)          (recv[21] & 0x08)
#define XHFK_CLIP_THREE(xhfk)        (recv[22] & 0x08)
#define XHFK_CLIP_FOUR(xhfk)         (recv[21] & 0x40)
#define XHFK_SCISS_ONE(xhfk)         (recv[21] & 0x80)
#define XHFK_SCISS_TWO(xhfk)         (recv[22] & 0x40)
#define XHFK_QDB_HIGH(xhfk)          (recv[22] & 0x20)
#define XHFK_QDB_SECURY(xhfk)        (recv[22] & 0x10)
#define XHFK_QDB_ZERO(xhfk)          (recv[21] & 0x10)
#define XHFK_SAFE_WAIT(xhfk) 	     (recv[21] & 0x01)
#define XHFK_RAB_OPEN(xhfk) 	     (recv[21] & 0x04)
#define XHFK_RAB_CLOSE(xhfk) 	     (recv[21] & 0x20)

//rock,前板为偶数，后板奇数
#define ROCK_MODE_FRONT     0//前板摇床
#define ROCK_MODE_BACK      1//后板摇床
#define ROCK_MODE_BOTH      2//前后摇床
#define ROCK_DOUBLE_BACK    3//双摇床，后板
#define ROCK_DOUBLE_FRONT   4//双摇床，前板

//uchar g_recvdata[DSP2ARM_SIZE];
//#define GetRecvSign()  g_recvdata

//16-20 needle count

#define FORMAT_MH        1
#define FORMAT_MH_A      2
#define FORMAT_MH_H      3

#define WIDEBYTES(cx)    ((cx+3)/4*4)
#define CANTIMES         1000

#ifndef NULL
#define NULL  0
#endif

#ifndef FALSE
#define FALSE  0
#define TRUE   1
#endif
//mouse
#define MOUSE_LEFT_DOWN    1
#define MOUSE_RIGHT_DOWN   2
#define MOUSE_MIDDLE_DOWN  4
#define MOUSE_HOVER        8
//button
#define BUTTON_UNCHECKED   0
#define BUTTON_CHECKED     1
//section change item
#define SECCHG_DUMU        0
#define SECCHG_SUDU        1
#define SECCHG_LABU        2
#define SECCHG_FULABU      3
#define SECCHG_KAIHE       4
#define SECCHG_QDLL        5
#define SECCHG_DMBZ        6
#define SECCHG_YARNSTOP    7
#define SECCHG_SINK        8

#define I2A(a, i) { sprintf(a, "%d", i); }
#define FREE(p) \
{\
    if(p) \
    { \
        free(p); \
        p = NULL; \
    } \
}
#define TEXT_SMALL_FONT      8
#define TEXT_MIDDLE_FONT     16
#define TEXT_INTERVAL        2
#define TEXT_ALIGN_LEFT      0
#define TEXT_ALIGN_MIDDLE    1
#define TEXT_ALIGN_RIGHT     2
#define ASCFILE_LENGTH       4096
#define FONTFILE_LENGTH      267616
#define MARGIN_LEFT          20
#define MARGIN_RIGHT         20
#define MARGIN_TOP           30
#define MARGIN_BOTTOM        15

#ifndef max
#define max(a, b)   ((a) > (b) ? (a) : (b))
#define min(a, b)   ((a) < (b) ? (a) : (b))

#endif

#define RGB_U16(r,g,b)       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
#define RGB_24To32(r, g, b)  (0xFF000000 | ((r) << 16) | ((g) << 8) | (b))
//
#define COLOR_BACKGROUND     7
//
#define COLOR_BORDER         252//4
//
#define COLOR_TITLE          4
//
#define COLOR_LABEL          4
//
#define COLOR_FRAME          255
//
#define COLOR_WARNNING       1
//
#define BORDER_WIDTH         6//5
//
#define COLOR_RED            10
#define COLOR_2RED           1
#define COLOR_GREEN          2
#define COLOR_BLUE           4
#define COLOR_WHITE          255
#define COLOR_BLACK          0
#define COLOR_YELLOW         3
#define COLOR_GRAD           248
#define COLOR_GRAY           192
#define COLOR_DGREEN         137
#define COLOR_BUTTON         252
#define COLOR_38             38
#define COLOR_104	     104   //灰
#define COLOR_2		     22	   //淡绿
#define COLOR_58	     58	   //深绿
#define COLOR_15	     15    //淡蓝
#define COLOR_11	     11    //黄
#define COLOR_105	     105   //粉红
#define COLOR_46 	     46	   //亮绿
#define COLOR_12	     12	   //
#define COLOR_251	     251   //暗黄(用作已使用的数据)
#define COLOR_249	     249   //红
#define COLOR_52	     52    //背景蓝
#define COLOR_59	     59   //主背景
#define COLOR_57	     57   //master_color
#define COLOR_190	     190  //亮白（主体字色）
#define COLOR_235	     235  //暗白
#define COLOR_89         89

//global id
#define MH_ID_NUM0       10000
#define MH_ID_NUM1       10001
#define MH_ID_NUM2       10002
#define MH_ID_NUM3       10003
#define MH_ID_NUM4       10004
#define MH_ID_NUM5       10005
#define MH_ID_NUM6       10006
#define MH_ID_NUM7       10007
#define MH_ID_NUM8       10008
#define MH_ID_NUM9       10009
#define MH_ID_A		 10100
#define MH_ID_B		 10101
#define MH_ID_C		 10102
#define MH_ID_D		 10103
#define MH_ID_E		 10104
#define MH_ID_F		 10105
#define MH_ID_SIGN       11001
#define MH_ID_DOT        11002
#define MH_ID_CLEAR      11003
#define MH_ID_BACKSPACE  11004
#define MH_ID_OK         11005
#define MH_ID_CANCEL     11006
#define MH_ID_RETURN     11007
#define MH_ID_BUTTON     12000
#define MH_ID_ZOOMIN     12001
#define MH_ID_ZOOMOUT    12002
//
#if IFLINK == 1
#define IFLINK_ID_IMAGE      20000
#define IFLINK_ID_BUTTON     20010
#endif
//icon
#define ICON_WARNING   32001

//system_alarm
#define		ALARM_N		100 // 系统报警个数
#define FLAG_YARN_IN    0x81
#define FLAG_YARN_OUT   0x82

#define RECV_EXTRA_DATA(recv) { DEBUG_OUT("%s %d receive extra data: %02X, %02X, %02X, %02X\n", __FILE__, __LINE__, recv[0], recv[1], recv[2], recv[3]); }


//最大值
#define MAX_MOTOR_SPEED         140 //机头最大速度


void* mh_read_file (const char *path, int size);

extern int g_update_complete;       //update or not

extern int g_lang;
extern char g_image_path[128];
//extern pthread_t g_udp_mutex;
extern pthread_mutex_t g_udp_mutex;

#endif
